package com.neu.question.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.neu.question.common.utils.UUIDUtil;
import com.neu.question.domain.QuestionInfo;
import com.neu.question.mapper.QuestionInfoMapper;
import com.neu.question.service.QuestionInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author nxwr0
 * @description 针对表【question_info】的数据库操作Service实现
 * @createDate 2022-11-04 12:25:24
 */
@Service
public class QuestionInfoServiceImpl extends ServiceImpl<QuestionInfoMapper, QuestionInfo>
        implements QuestionInfoService {

    @Autowired
    private QuestionInfoMapper questionEntityMapper;

    @Override
    public int addQuestion(Map<String, Object> map) {
        String questionJson = (String) map.get("questionList");
        //[{"questionType":"0","questionTitle":"你最近一个星期的课外活动频率如何","questionOption":[{"lineTitle":"",
        // "optionWord":"10小时以上","optionGrade":""},{"lineTitle":"","optionWord":"5-10小时","optionGrade":""},
        // {"lineTitle":"","optionWord":"2-5小时","optionGrade":""},{"lineTitle":"","optionWord":"2小时以下",
        // "optionGrade":""}],"important":"取消必答题"},{"questionType":"2","questionTitle":"请列举你目前的课外活动有哪些",
        // "questionOption":[],"important":"必答题"}]
        //将json数组的字符串转换为map
        List<Map<String, Object>> questionList = JSON.parseObject(questionJson, new TypeReference<List<Map<String,
                Object>>>() {
        });
//遍历questionList
        for (Map<String, Object> questionMap : questionList) {
            //定义id
            String id = UUIDUtil.getOneUUID();
            questionMap.put("id", id);
            //处理questionOption
            Object questionOption = questionMap.get("questionOption");
            String questionOptionStr = JSON.toJSONString(questionOption);
            questionMap.put("questionOption", questionOptionStr);
            //将questionInfo插入数据库
            questionEntityMapper.addQuestion(questionMap);
        }
        return 1;
    }

    @Override
    public PageInfo getQuestionByTitle(Map<String, Object> map) {
        //分页设置
        PageHelper.startPage((Integer) map.get("pageNum"), (Integer) map.get("pageSize"));
        List<Map<String, Object>> mapList = questionEntityMapper.getQuestionByTitle(map);
        PageInfo<Map<String, Object>> objectPageInfo = new PageInfo<>(mapList);
        return objectPageInfo;
    }

    @Override
    public List<Map<String, Object>> getAllQuestion(Map<String, Object> map) {
        List<Map<String, Object>> mapList = questionEntityMapper.getQuestionByTitle(map);
        return mapList;
    }

    @Override
    public int updateQuestion(Map<String, Object> map) {
        //删除原有的题目
        questionEntityMapper.deleteAllQuestion();
        //添加新的题目
        addQuestion(map);
        return 1;
    }

    @Override
    public int deleteQuestion(Map<String, Object> map) {
        questionEntityMapper.deleteQuestion(map);
        return 1;
    }

    @Override
    public List<Map<String, Object>> getQuestionByIdList(Map<String, Object> map) {
        //创建一个list
        List<Map<String, Object>> mapList =new ArrayList<>();
        //获取id列表
        String idList = (String) map.get("idList");
        //将idList转换为list
        List<String> idList1 = JSON.parseObject(idList, new TypeReference<List<String>>() {
        });
        //遍历idList
        for (String id : idList1) {
            //根据id获取题目
            Map<String, Object> questionById = questionEntityMapper.getQuestionById(id);
            //处理questionOption
            String questionOption = (String) questionById.get("questionOption");
            List<Map<String, Object>> questionOptionList = JSON.parseObject(questionOption, new TypeReference<List<Map<String,
                    Object>>>() {
            });
            questionById.put("questionOption", questionOptionList);
            //将题目添加到list中
            mapList.add(questionById);
        }
        return mapList;
    }
}




